package com.oba.king.datalib;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.event.AbortProcessingException;
import javax.faces.model.SelectItem;

import com.icesoft.faces.component.paneltabset.TabChangeEvent;
import com.oba.base.dao.DBDataModel;
import com.oba.king.datalib.creditcontrol.CreditControl;
import com.oba.king.sysmanager.area.Area;

/**
 * 
 * @author 大饼
 * 客户信息管理
 */
public class CustomerBean extends com.oba.base.service.BaseBean<Customer, CustomerService>{
          private DBDataModel customerData= DBDataModel.create();
          private Customer customer;
          private boolean showCustomerInfo;
          
        //查询 条件
      	private CustomerCondition customerCondition;
      	// 片区选择列表
      	private List<SelectItem> areaList;
      	private List<SelectItem> queryAreaList;
      	//信用等级选择列表
      	private List<SelectItem> creditControlList;
      	private RecieverInfoBean recieverInfoBean;

      	//paneltab 相关属性
      	private String selectedIndex="0";
      	 /**
         * Tabbed placement, possible values are "top" and "bottom", the default is
         * "bottom".
         */
      	private String tabPlacement="top";
      	private boolean customerVisible ;
      	private boolean recieverVisible;
      	

		public void doAddRow() {
      		
      		customer = new Customer();
      		customer.setNewBo(true);
    		AccountValues av = new AccountValues();
    		av.setLastUpdateDate(new Date());
    		av.setNewBo(true);
    		customer.setAccountValue(av);
    		showCustomerInfo = true;
    	}
      	

		public List<SelectItem> getAreaList() {
			return areaList;
		}

		public void setAreaList(List<SelectItem> areaList) {
			this.areaList = areaList;
		}

		public void initAreaList(){
       		areaList =new ArrayList<SelectItem>();
    		
    		for(Area a : getService().getDao().findAllArea()){
    			areaList.add(new SelectItem(a,a.getAreaName()));
    		}
           }
		private void initCreditControlList(){
			creditControlList=new ArrayList<SelectItem>();
			for(CreditControl a : getService().getDao().findAllCreditControl()){
				creditControlList.add(new SelectItem(a,a.getCreditRate().toString()));
    		}
		}
		private void initQueryAreaList(){
			queryAreaList =new ArrayList<SelectItem>();
			queryAreaList.add(new SelectItem(null,"全部"));
			queryAreaList.addAll(areaList);
		}
    	
    	public void doEditRow() {
    		if(customerData.isRowAvailable()) {
    			customer = (Customer) customerData.getRowData();
    			showCustomerInfo = true;
    			doFind();
    		}
    	}
    	
    	
    	public void doDeleteRow() {
    		if(customerData.isRowAvailable()) {
    			customer = (Customer) customerData.getRowData();
    			getService().del(customer);
    			doFind();
    		}
    	}
    	
    	public void closeShowEdit() {
    		showCustomerInfo = false;
    	}
    	
    	public void doSaveRow() {
    		// TODO
    		getService().save(customer.getAccountValue());
    		getService().save(customer);
    		showCustomerInfo = false;
    		doFind();
    	}

    	@Override
    	public String doFind() {
    		customerData.find(getService().getDao().findAll());
    		return null;
    	}
    	
    	public void clearCustomerCondition(){
    		customerCondition=new CustomerCondition();
    	}
    	
    	public void findByCustomer(){
    		customerData.find(getService().getDao().findByCustomer(customerCondition));
    	}

	@Override
	public void init() {
		customerData.find(getService().getDao().findAll());
		customer = new Customer();
		customerCondition =new CustomerCondition();
		showCustomerInfo = false;
		initAreaList();
  		initCreditControlList();
  		initQueryAreaList();
  		recieverInfoBean.init();
	}

	public DBDataModel getCustomerData() {
		return customerData;
	}

	public void setCustomerData(DBDataModel customerData) {
		this.customerData = customerData;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public boolean isShowCustomerInfo() {
		return showCustomerInfo;
	}

	public void setShowCustomerInfo(boolean showCustomerInfo) {
		this.showCustomerInfo = showCustomerInfo;
	}


	public CustomerCondition getCustomerCondition() {
		return customerCondition;
	}


	public void setCustomerCondition(CustomerCondition customerCondition) {
		this.customerCondition = customerCondition;
	}


	public List<SelectItem> getQueryAreaList() {
		return queryAreaList;
	}


	public void setQueryAreaList(List<SelectItem> queryAreaList) {
		this.queryAreaList = queryAreaList;
	}

	public RecieverInfoBean getRecieverInfoBean() {
		return recieverInfoBean;
	}


	public void setRecieverInfoBean(RecieverInfoBean recieverInfoBean) {
		this.recieverInfoBean = recieverInfoBean;
	}


	public List<SelectItem> getCreditControlList() {
		return creditControlList;
	}


	public void setCreditControlList(List<SelectItem> creditControlList) {
		this.creditControlList = creditControlList;
	}


	public String getSelectedIndex() {
		return selectedIndex;
	}


	public void setSelectedIndex(int selectedIndex) {
		this.selectedIndex = String.valueOf(selectedIndex);
	}


	public String getTabPlacement() {
		return tabPlacement;
	}


	public void setTabPlacement(String tabPlacement) {
		this.tabPlacement = tabPlacement;
	}


	public boolean isCustomerVisible() {
		return customerVisible;
	}


	public void setCustomerVisible(boolean customerVisible) {
		this.customerVisible = customerVisible;
	}


	public boolean isRecieverVisible() {
		return recieverVisible;
	}


	public void setRecieverVisible(boolean recieverVisible) {
		this.recieverVisible = recieverVisible;
	}
	
	public int getFocusIndex() {
        int focusIndex = 0;
        try{
            focusIndex = Integer.parseInt(selectedIndex);
        }
        catch(NumberFormatException nfe){
            //do nothing
        }
        return focusIndex;
    }
	public void setFocusIndex(int index){
        selectedIndex = String.valueOf(index);
    }
	
	/**
     * Called when the table binding's tab focus changes.
     *
     * @param tabChangeEvent used to set the tab focus.
     * @throws AbortProcessingException An exception that may be thrown by event
     *                                  listeners to terminate the processing of the current event.
     */
    public void processTabChange(TabChangeEvent tabChangeEvent)
            throws AbortProcessingException {
        
    }
	
}
